package me.gerry.locktest.manager;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;

import me.gerry.locktest.view.FloatView;

/**
 * Created by gerry on 2018/6/24.
 */

public class FloatViewMgr {
    private FloatView floatView;
    private static final FloatViewMgr ourInstance = new FloatViewMgr();
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams wmParams;

    public static FloatViewMgr getInstance() {
        return ourInstance;
    }

    private FloatViewMgr() {
    }

    public void initWindowManager(Context context){
        if (mWindowManager == null)
            mWindowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        if (wmParams == null) {
            wmParams = new WindowManager.LayoutParams();
            // 设置window type
            wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
            // 设置图片格式，效果为背景透明
            wmParams.format = PixelFormat.TRANSLUCENT;
            // 设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
            wmParams.flags = (WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | wmParams.flags);
            // 如果是android 4.4以上，通知栏可透明，忽略通知栏高度
            if (Build.VERSION.SDK_INT >= 19)
                wmParams.flags = (WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | wmParams.flags);
            int flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_FULLSCREEN;
            // 0x01000000为硬件加速flag，防止界面卡顿
            wmParams.flags = flags | 0x01000000 | wmParams.flags;

            wmParams.gravity = Gravity.LEFT | Gravity.TOP;

            // 设置悬浮窗口长宽数据
            wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        }
    }

    public void showFloatView(Context context){
        initWindowManager(context);
        if (floatView == null){
            floatView = new FloatView(context);
            floatView.setBackgroundColor(Color.RED);
        }
        mWindowManager.addView(floatView,wmParams);
    }
}
